package com.visionork.android.tai;

import java.util.ArrayList;
import java.util.Collection;

import com.visionork.android.tai.activity.R;
import com.visionork.android.tai.model.DramaProgramRepository;
import com.visionork.android.tai.model.FavoriteProgramRepository;
import com.visionork.android.tai.model.ProgramRepositoryBase;
import com.visionork.android.tai.model.ShowProgramRepository;
import com.visionork.android.tai.model.Episode;
import com.visionork.android.tai.model.adapter.TaiAdapterBase;
import com.visionork.android.tai.model.adapter.WhatsNewAdapter;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

public class TaiApp extends Application {
	private final static String FEEDURL = "http://feeds.feedburner.com/twsugoideas?format=xml";
	private static Context context;
	private static final ShowProgramRepository showRepository;
	private static final DramaProgramRepository dramaRepository;
	private static final FavoriteProgramRepository favoriteRepository;
	private static final ArrayList<Episode> latestEpisodes;
	private static final TaiAdapterBase whatsNewAdapter;
	static {
		showRepository = new ShowProgramRepository();
		dramaRepository = new DramaProgramRepository();
		favoriteRepository = new FavoriteProgramRepository();
		latestEpisodes = new ArrayList<Episode>();
		whatsNewAdapter = new WhatsNewAdapter(FEEDURL);
	}

	@Override
	public void onCreate() {
		TaiApp.context = getApplicationContext();
	}

	@Override
	public void onTerminate() {
		showRepository.saveToFile();
		dramaRepository.saveToFile();
	}

	@Override
	public void onLowMemory() {
		showRepository.saveToFile();
		dramaRepository.saveToFile();
	}

	public static Context getAppContext() {
		if (context == null) {

		}
		return context;
	}

	public static ProgramRepositoryBase getShowRepository() {
		if (showRepository.getProgramsRef().size() == 0) {
			showRepository.loadFromFile();
		}
		return showRepository;
	}

	public static ProgramRepositoryBase getDramaRepository() {
		if (dramaRepository.getProgramsRef().size() == 0) {
			dramaRepository.loadFromFile();
		}
		return dramaRepository;
	}

	public static FavoriteProgramRepository getFavoriteRepository() {
		return favoriteRepository;
	}

	public static ArrayList<Episode> getLatestEpisodes() {
		return getLatestEpisodes(true);
	}

	public static Episode getLatestEpisodeByUrlLink(String urlLink) {
		for (Episode epi : latestEpisodes) {
			if (epi.getUrlLink().equals(urlLink)) {
				return epi;
			}
		}
		return null;
	}

	public static ArrayList<Episode> getLatestEpisodes(boolean useCache) {
		if (!useCache) {
			@SuppressWarnings("unchecked")
			Collection<? extends Episode> data = (Collection<? extends Episode>) whatsNewAdapter
					.getData();
			mergeData(data);
		}
		return latestEpisodes;
	}

	private static void mergeData(Collection<? extends Episode> data) {

		for (Episode epi : data) {
			if (!latestEpisodes.contains(epi)) {
				/* add new one */
				latestEpisodes.add(epi);
			} else {
				/* update old one */
				Episode existEpi = latestEpisodes.get(latestEpisodes
						.indexOf(epi));
				existEpi.setTitle(epi.getTitle());
			}
		}

		/* remove expired one */
		for (Episode epi : latestEpisodes) {
			if (!data.contains(epi)) {
				latestEpisodes.remove(epi);

			}
		}

	}

	public static ArrayList<Episode> getLatestEpisodesRef() {
		return latestEpisodes;
	}

	public static void share(Activity activity, String subject, String text) {
		Resources res = activity.getResources();
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		intent.putExtra(Intent.EXTRA_TEXT, text);
		activity.startActivity(Intent.createChooser(intent,
				res.getString(R.string.share)));
	}

	public static void loadData() {
		getShowRepository().loadFromFile();
		getDramaRepository().loadFromFile();
	}
}
